// <copyright file="AppDbContext.cs" company="EnterpriseApi">
// Copyright (c) EnterpriseApi. All rights reserved.
// </copyright>

using EnterpriseApi.Models;
using Microsoft.EntityFrameworkCore;

namespace EnterpriseApi.Data;

/// <summary>
/// 应用程序数据库上下文，用于Entity Framework Core与MySQL数据库的交互
/// </summary>
public class AppDbContext : DbContext
{
    /// <summary>
    /// 初始化AppDbContext的新实例
    /// </summary>
    /// <param name="options">数据库上下文配置选项</param>
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    /// <summary>
    /// 用户表
    /// </summary>
    public DbSet<User> Users { get; set; }

    /// <summary>
    /// 角色表
    /// </summary>
    public DbSet<Role> Roles { get; set; }

    /// <summary>
    /// 产品表
    /// </summary>
    public DbSet<Product> Products { get; set; }

    /// <summary>
    /// 类别表
    /// </summary>
    public DbSet<Category> Categories { get; set; }

    /// <summary>
    /// 配置实体映射和关系
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // 配置User实体
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(u => u.Id);
            entity.Property(u => u.Username).IsRequired().HasMaxLength(50);
            entity.Property(u => u.PasswordHash).IsRequired().HasMaxLength(255);
            entity.Property(u => u.Email).IsRequired().HasMaxLength(100);
            entity.Property(u => u.PhoneNumber).HasMaxLength(20);
            entity.HasIndex(u => u.Username).IsUnique();
            entity.HasIndex(u => u.Email).IsUnique();

            // 配置与Role的关系
            entity.HasOne(u => u.Role)
                  .WithMany(r => r.Users)
                  .HasForeignKey(u => u.RoleId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        // 配置Role实体
        modelBuilder.Entity<Role>(entity =>
        {
            entity.HasKey(r => r.Id);
            entity.Property(r => r.Name).IsRequired().HasMaxLength(50);
            entity.Property(r => r.Description).HasMaxLength(255);
            entity.HasIndex(r => r.Name).IsUnique();
        });

        // 配置Product实体
        modelBuilder.Entity<Product>(entity =>
        {
            entity.HasKey(p => p.Id);
            entity.Property(p => p.Name).IsRequired().HasMaxLength(100);
            entity.Property(p => p.Description).HasMaxLength(1000);
            entity.Property(p => p.Price).HasPrecision(18, 2);
            entity.Property(p => p.ImageUrl).HasMaxLength(500);

            // 配置与Category的关系
            entity.HasOne(p => p.Category)
                  .WithMany(c => c.Products)
                  .HasForeignKey(p => p.CategoryId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        // 配置Category实体
        modelBuilder.Entity<Category>(entity =>
        {
            entity.HasKey(c => c.Id);
            entity.Property(c => c.Name).IsRequired().HasMaxLength(50);
            entity.Property(c => c.Description).HasMaxLength(255);
            entity.HasIndex(c => c.Name).IsUnique();
        });

        // 配置种子数据
        SeedData(modelBuilder);
    }

    /// <summary>
    /// 配置数据库种子数据
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    private void SeedData(ModelBuilder modelBuilder)
    {
        // 种子角色数据
        modelBuilder.Entity<Role>().HasData(
            new Role { Id = 1, Name = "Admin", Description = "系统管理员", CreatedAt = DateTime.UtcNow },
            new Role { Id = 2, Name = "User", Description = "普通用户", CreatedAt = DateTime.UtcNow },
            new Role { Id = 3, Name = "Guest", Description = "访客用户", CreatedAt = DateTime.UtcNow }
        );

        // 种子类别数据
        modelBuilder.Entity<Category>().HasData(
            new Category { Id = 1, Name = "电子产品", Description = "各类电子产品", CreatedAt = DateTime.UtcNow },
            new Category { Id = 2, Name = "服装鞋帽", Description = "各类服装和鞋帽", CreatedAt = DateTime.UtcNow },
            new Category { Id = 3, Name = "食品饮料", Description = "各类食品和饮料", CreatedAt = DateTime.UtcNow }
        );
    }
}